package com.wangyb.wework.controller;

import com.wangyb.wework.entity.CorpEntity;
import com.wangyb.wework.utils.WxTokenUtils;
import com.wangyb.wework.utils.crypt.WXCallbackMsgCrypt;
import com.wangyb.wework.wxApi.callback.WxCallbackMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangyb
 * @Date: 2021/8/5 3:38 下午
 */
@Slf4j
@RestController
public class WwCallbackController {
	@Autowired
	private RedissonClient redissonClient;

	@GetMapping(value="/wework/callback/{corpId}", produces = {"text/html"})
	public String verifyURL(String msg_signature,
							String timestamp,
							String nonce,
							String echostr,
							@PathVariable("corpId")String cid) throws Exception {
		msg_signature = processArg(msg_signature);
		timestamp = processArg(timestamp);
		nonce = processArg(nonce);
		echostr = processArg(echostr);
		cid = processArg(cid);

		log.debug(">>>>>>> msg_signature: " + msg_signature);
		log.debug(">>>>>>> timestamp: " + timestamp);
		log.debug(">>>>>>> nonce: " + nonce);
		log.debug(">>>>>>> echostr: " + echostr);
		log.debug(">>>>>>> corpId: " + cid);

		Long corpId = Long.valueOf(cid);
		CorpEntity corp = WxTokenUtils.getCorpEntity(corpId);
		String token = corp.getToken();
		String wxCorpId = corp.getWxCorpId();
		String encodingAesKey = corp.getEncodingAesKey();

		WXCallbackMsgCrypt msgCrypt = new WXCallbackMsgCrypt(token, encodingAesKey, wxCorpId);
		// 验证微信回调参数是否有效
		String sEchoStr = msgCrypt.VerifyURL(msg_signature, timestamp, nonce, echostr);

		//必须要返回解密之后的明文
		if (StringUtils.isBlank(sEchoStr)) {
			log.debug("URL验证失败");
		} else {
			log.debug("验证成功!");
		}

		log.debug(" >>>>>>>> sEchoStr: " + sEchoStr);

		return sEchoStr;

	}
	/**
	 * 企业微信客户联系回调.
	 *
	 * @return success
	 */
	@PostMapping(value = "/wework/callback/{corpId}")
	public String callback(@RequestBody String requestBody, String msg_signature, String timestamp, String nonce, @PathVariable("corpId")String cid) throws Exception {

		log.debug("<<<<<<<<< requestBody: " + requestBody);
		log.debug("<<<<<<<<< msg_signature: " + msg_signature);
		log.debug("<<<<<<<<< timestamp: " + timestamp);
		log.debug("<<<<<<<<< nonce: " + nonce);

		requestBody = processArg(requestBody);
		msg_signature = processArg(msg_signature);
		timestamp = processArg(timestamp);
		nonce = processArg(nonce);
		cid = processArg(cid);
		Long corpId = Long.valueOf(cid);
		CorpEntity corp = WxTokenUtils.getCorpEntity(corpId);
		String token = corp.getToken();
		String wxCorpId = corp.getWxCorpId();
		String encodingAesKey = corp.getEncodingAesKey();

		WXCallbackMsgCrypt msgCrypt = new WXCallbackMsgCrypt(token, encodingAesKey, wxCorpId);

		String sMsg = msgCrypt.DecryptMsg(msg_signature, timestamp, nonce, requestBody);

		log.debug(" >>>>>>>> 微信回调信息: " + sMsg);

		WxCallbackMsg callbackMsg = WxCallbackMsg.parse(sMsg);
		callbackMsg.setCorpId(corpId);

		RTopic topic = redissonClient.getTopic(callbackMsg.getEvent().name());
		topic.publish(callbackMsg);

//		return dispatcher.dispatch(callbackMsg.getEvent(),callbackMsg);
		return "success";
	}
	private String processArg(String arg) {
		int index = arg.indexOf(",");
		if (index > 0) {
			return arg.substring(index + 1);
		}
		return arg;
	}

}
